﻿using System;
using System.Collections.Generic;
using System.Web;

namespace IntelligentCMS
{
    /// <summary>
    /// Role class, for specifying a role, as well as which users are a part of it.
    /// </summary>
    public class CMSRole
    {
        public CMSRole(string name)
        {
            this.name = name;
            GetRoleFromDB();
        }

        private string name = string.Empty;
        private int permissions = 0;
        private List<string> users = new List<string>();


        /// <summary>
        /// Switching to UNIX style permissions. 777 should give access to everything.
        /// </summary>
        public int Permissions
        {
            get
            {
                return permissions;
            }
           

        }


        private void GetRoleFromDB() 
        {
            /*
            CMSDbDataLayer newDataLayer = new CMSDbDataLayer(string.Empty, CMSDbDataHelper.Microsoft);
            newDataLayer.AddQuery("SELECT [Permissions] FROM CMS_Roles WHERE [RoleName] = '" + name + "'");
            newDataLayer.NonQuery = true;
            newDataLayer.Execute();

            permissions = int.Parse(newDataLayer.Data.Rows[0].ItemArray[0].ToString());

            */
        }

        

        /// <summary>
        /// Adds the user.
        /// </summary>
        /// <param name="user">The user.</param>
        public void AddUser(string username)
        {
            users.Add(username);
            // todo: add db code
        }

        /// <summary>
        /// Removes the user.
        /// </summary>
        /// <param name="user">The user.</param>
        public void RemoveUser(string username)
        {
            int index = -1;
            for (int i = 0; i < users.Count; i++ )
            {
                if (users[i] == username)
                {
                    index = i;
                }

            }

            if (index > -1)
            {
                users.RemoveAt(index);

            }
            // todo: add db code

        }

        


        /// <summary>
        /// Checks if the user has permission for a given action, given the role it is a part of.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <returns></returns>
        public bool CheckUser(string username)
        {
            foreach (string s in users)
            {
                if (s == username)
                {
                    return true;
                }

            }

            return false;
        }


    }
}
